package com.xfatm.simplebrowser.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.xfatm.simplebrowser.entity.BrowserSetting;
import com.xfatm.simplebrowser.util.BeanManageUtils;

/**
 * Created by wenjie on 2018/3/11.
 */

public class MainWebView extends WebView{
    public MainWebView(Context context) {
        super(context);
    }

    public MainWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MainWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public MainWebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init(){
        WebSettings settings=getSettings();
        settings.setJavaScriptEnabled(true);
        /**设置webView可以打开窗体*/
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        /**设置不显示滚动条*/
        //base_webview.setVerticalScrollBarEnabled(false);
        //base_webview.setHorizontalScrollBarEnabled(false);
        /**设置网页字体不跟随系统字体发生改变*/
        settings.setTextZoom(100);
        /**设置默认编码格式*/
        settings.setDefaultTextEncodingName("utf-8");
        /**设置可以调用数据库*/
        settings.setDatabaseEnabled(true);
        /**设置dom存储*/
        settings.setDomStorageEnabled(true);
        // 自适应屏幕
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        //支持缩放，默认为true。
        settings .setSupportZoom(false);
        //调整图片至适合webview的大小
        settings .setUseWideViewPort(true);
        // 缩放至屏幕的大小
        settings .setLoadWithOverviewMode(true);
        //自动加载图片
        settings.setLoadsImagesAutomatically(true);
        //支持访问文件
        settings.setAllowFileAccess(true);
        settings.setPluginState(WebSettings.PluginState.ON);

        MainWebViewClient client=new MainWebViewClient();
        setWebViewClient(client);
        setWebChromeClient(new MainWebChromeClient());
    }


    //滑动事件


    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        if(!isFocused()){
            setFocusable(true);
            setFocusableInTouchMode(true);
            requestFocus();
            requestFocusFromTouch();
        }
        super.onScrollChanged(l, t, oldl, oldt);

        //是否需要自动隐藏地址栏
        LinearLayout linearLayout= BeanManageUtils.getBean("headLayout",LinearLayout.class);
        if(BrowserSetting.getSetting().isAutoHideHead()){
            int move=(oldt-t);
            //为了避免闪动，两次滑动方向必须一样
            int lastMove=BeanManageUtils.getInt("lastMove");
            BeanManageUtils.saveBean("lastMove",move);
            if(!((move>0&&lastMove>0)||(move<0&&lastMove<0))){
                move=0;
            }
            //隐藏头部
            if(linearLayout.getTop()*-1<linearLayout.getHeight()&&move<0){
                if((linearLayout.getTop()+move)*-1>linearLayout.getHeight()){
                    move=(linearLayout.getHeight()+linearLayout.getTop())*-1;
                }
                LinearLayout.LayoutParams layoutParams=(LinearLayout.LayoutParams)linearLayout.getLayoutParams();
                layoutParams.topMargin+=move;
                linearLayout.setLayoutParams(layoutParams);
            }
            //显示头部
            if(linearLayout.getTop()<0&&move>0){
                if(linearLayout.getTop()+move>0){
                    move=linearLayout.getTop()*-1;
                }
                LinearLayout.LayoutParams layoutParams=(LinearLayout.LayoutParams)linearLayout.getLayoutParams();
                layoutParams.topMargin+=move;
                linearLayout.setLayoutParams(layoutParams);
            }
        }else if(linearLayout.getTop()<0){
            LinearLayout.LayoutParams layoutParams=(LinearLayout.LayoutParams)linearLayout.getLayoutParams();
            layoutParams.topMargin-=linearLayout.getTop();
            linearLayout.setLayoutParams(layoutParams);
        }


    }
}
